package com.tyrone.concurrent.lock;

import com.tyrone.concurrent.basic.ThreadState;
import com.tyrone.concurrent.utils.SleepUtils;

/**
 * @author xieyl
 * @create 2021/12/9 21:58
 */
public class TwinsLockTest {
    public static void main(String[] args) {
        final TwinsLock twinsLock = new TwinsLock();
        class Worker extends Thread {
            @Override
            public void run() {
                while (true) {
                    twinsLock.lock();
                    try{
                        SleepUtils.second(1);
                        System.out.println(Thread.currentThread().getName());
                        SleepUtils.second(1);
                    }finally {
                        twinsLock.unlock();
                    }
                }
            }
        }
        for (int i = 0; i < 10; i++) {
            Worker worker = new Worker();
            worker.setDaemon(true);
            worker.start();
        }
        for (int i = 0; i < 10; i++) {
            SleepUtils.second(1);
            System.out.println();;
        }
    }

}
